home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_1430.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  2.0 KB  |  15 lines

  1. The easiest way to see the distinction is by an analogy with 'virtual fns': A virtual member fn means the declaration (signature) must stay the same in subclasses, but the defn (body) can be overridden.  The overriddenness of an inherited member fn is a static property of the subclass; it doesn't change dynamically throughout the life of any particular object, nor is it possible for distinct objects of the subclass to have distinct defns of the member fn.
  2.  
  3. Now go back and re-read the previous paragraph, but make these substitutions:
  4.     'member fn' --> 'subobject'
  5.     'signature' --> 'type'
  6.     'body'      --> 'exact class'
  7. After this, you'll have a working defn of virtual data.
  8.  
  9. 'Per-object member fns' (a member fn 'f()' which is potentially different in any given instance of an object) could be handled by burying a function ptr in the object, then setting the (const) fn ptr during construction.
  10.  
  11. 'Dynamic member fns' (member fns which change dynamically over time) could also be handled by function ptrs, but this time the fn ptr would not be const.
  12.  
  13. In the same way, there are three distinct concepts for data members:  * virtual data: the defn ('class') of the subobject is overridable in    subclasses provided its declaration ('type') remains the same, and this    overriddenness is a static property of the [sub]class.  * per-object-data: any given object of a class can instantiate a different    conformal (same type) subobject upon initialization (usually a 'wrapper'    object), and the exact class of the subobject is a static property of the    object that wraps it.  * dynamic-data: the subobject's exact class can change dynamically over time.
  14.  
  15. The reason they all look so much the same is that none of this is 'supported' in C++.  It's all merely 'allowed', and in this case, the mechanism for faking each of these is the same: a ptr to a (probably abstract) base class.  In a language that made these 'first class' abstraction mechanisms, the difference would be more striking, since they'd each have a different syntactic variant.